package com.ph.irp.base.verifyproject.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ph.irp.base.accountbook.dao.PBaseAccountbookDAO;
import com.ph.irp.base.materialassistantattribute.dao.AssistantAttributeTypeDao;
import com.ph.irp.base.measure.dao.PBaseMeasureUnitDAO;
import com.ph.irp.base.measureunitgroup.dao.PBaseMeasureUnitGroupDAO;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprogectSupplierDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCandiateDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectMaterialDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectStorageDao;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectMaterialService;
import com.ph.irp.dto.MaterialDTO;
import com.ph.irp.model.PBaseAccountbook;
import com.ph.irp.model.PBaseMaterialAssistantattributetype;
import com.ph.irp.model.PBaseMeasuregroup;
import com.ph.irp.model.PBaseMeasureunit;
import com.ph.irp.model.PBaseVerifyprojectCandiate;
import com.ph.irp.model.PBaseVerifyprojectMaterial;
import com.ph.irp.model.PBaseVerifyprojectStorage;
import com.ph.irp.model.PBaseVerifyprojectSupplier;

public class PBaseVerifyprojectMaterialServiceImpl implements
		PBaseVerifyprojectMaterialService {
	private PBaseVerifyprojectMaterialDao materialDaoProxy;
	private PBaseMeasureUnitGroupDAO measureUnitGroupDaoProxy;
	private PBaseMeasureUnitDAO measureUnitDaoProxy;
	private PBaseAccountbookDAO accountDaoProxy;
	private PBaseVerifyprojectCandiateDao candiateDaoProxy;
	private PBaseVerifyprojectStorageDao storageDaoProxy;
	private PBaseVerifyprogectSupplierDao supplierDaoProxy;
	private AssistantAttributeTypeDao assiatantAttributetypeDaoProxy;

	public void setMeasureUnitDaoProxy(PBaseMeasureUnitDAO measureUnitDaoProxy) {
		this.measureUnitDaoProxy = measureUnitDaoProxy;
	}

	@Override
	public void addMaterial(PBaseVerifyprojectMaterial material)
			throws Throwable {
		// if(materialDaoProxy.isSame(material.getMaterialCode())){
		// throw new Throwable("代码【"+material.getMaterialCode()+"】已经存在");
		// }else{
		materialDaoProxy.addMaterial(material);
		// }
	}

	@Override
	public void deleteMaterial(int materialId) throws Throwable {
		materialDaoProxy.deleteMaterial(materialId);
	}

	@Override
	public MaterialDTO getMaterialById(int materialId) throws Throwable {
		MaterialDTO m = new MaterialDTO();
		PBaseVerifyprojectMaterial material = materialDaoProxy
				.getMaterialById(materialId);
		int PBaseMeasuregroupId = material.getPBaseMeasuregroupId();
		int PBaseAccountbookBySalesInputAccountBookId = material
				.getPBaseAccountbookBySalesInputAccountBookId();
		int PBaseAccountbookByStorageAccountBookId = material
				.getPBaseAccountbookByStorageAccountBookId();
		int PBaseVerifyprojectSupplierId = material
				.getPBaseVerifyprojectSupplierId();
		int PBaseVerifyprojectStorageId = material
				.getPBaseVerifyprojectStorageId();
		int PBaseVerifyprojectCandiateId = material
				.getPBaseVerifyprojectCandiateId();
		int PBaseMaterialAssistantattributetypeId = material
				.getPBaseMaterialAssistantattributetypeId();
		int PBaseAccountbookBySalesCostAccountBookId = material
				.getPBaseAccountbookBySalesCostAccountBookId();
		m.setMaterial(material);
		if (PBaseMeasuregroupId != 0) {
			PBaseMeasuregroup measureGroup = measureUnitGroupDaoProxy
					.getMeasureGroupById(PBaseMeasuregroupId);
			m.setMeasureGroup(measureGroup);
		}
		if (PBaseAccountbookBySalesInputAccountBookId != 0) {
			PBaseAccountbook salesInputAccountBook = accountDaoProxy
					.getAccountbookById(PBaseAccountbookBySalesInputAccountBookId);
			m.setSalesInputAccountBook(salesInputAccountBook);
		}
		if (PBaseAccountbookByStorageAccountBookId != 0) {
			PBaseAccountbook storageAccountBook = accountDaoProxy
					.getAccountbookById(PBaseAccountbookByStorageAccountBookId);
			m.setStorageAccountBook(storageAccountBook);
		}
		if (PBaseVerifyprojectSupplierId != 0) {
			PBaseVerifyprojectSupplier supplier = supplierDaoProxy
					.getSupplierById(PBaseVerifyprojectSupplierId);
			m.setSupplier(supplier);
		}
		if (PBaseVerifyprojectStorageId != 0) {
			PBaseVerifyprojectStorage storage = storageDaoProxy
					.getStorageById(PBaseVerifyprojectStorageId);
			m.setStorage(storage);
		}
		if (PBaseVerifyprojectCandiateId != 0) {
			PBaseVerifyprojectCandiate candiate = candiateDaoProxy
					.getCandiateById(PBaseVerifyprojectCandiateId);
			m.setCandiate(candiate);
		}
		if (PBaseMaterialAssistantattributetypeId != 0) {
			/** 此处需要修改 */
			/** 王格滨2009-12-3添加* */
			// assistantattributetype = new
			// PBaseMaterialAssistantattributetype();
			PBaseMaterialAssistantattributetype assistantattributetype = assiatantAttributetypeDaoProxy
					.getAttrobuteTypeById(material
							.getPBaseMaterialAssistantattributetypeId());
			// assistantattributetype.setAssistantAttributeTypeName("辅助资料");
			m.setAssistantattributetype(assistantattributetype);
		}
		if (PBaseAccountbookBySalesCostAccountBookId != 0) {
			PBaseAccountbook salesCostAccountBook = accountDaoProxy
					.getAccountbookById(PBaseAccountbookBySalesCostAccountBookId);
			m.setSalesCostAccountBook(salesCostAccountBook);
		}
		return m;
	}

	@Override
	public List<MaterialDTO> getMaterialList(PBaseVerifyprojectMaterial material)
			throws Throwable {
		List<MaterialDTO> materials = new ArrayList<MaterialDTO>();
		List<PBaseVerifyprojectMaterial> list = materialDaoProxy
				.getMaterialList(material);
		for (PBaseVerifyprojectMaterial pbvm : list) {
			MaterialDTO m = new MaterialDTO();
			int PBaseMeasuregroupId = pbvm.getPBaseMeasuregroupId();
			int PBaseAccountbookBySalesInputAccountBookId = pbvm
					.getPBaseAccountbookBySalesInputAccountBookId();
			int PBaseAccountbookByStorageAccountBookId = pbvm
					.getPBaseAccountbookByStorageAccountBookId();
			int PBaseVerifyprojectSupplierId = pbvm
					.getPBaseVerifyprojectSupplierId();
			int PBaseVerifyprojectStorageId = pbvm
					.getPBaseVerifyprojectStorageId();
			int PBaseVerifyprojectCandiateId = pbvm
					.getPBaseVerifyprojectCandiateId();
			int PBaseMaterialAssistantattributetypeId = pbvm
					.getPBaseMaterialAssistantattributetypeId();
			int PBaseAccountbookBySalesCostAccountBookId = pbvm
					.getPBaseAccountbookBySalesCostAccountBookId();
			m.setMaterial(pbvm);
			if (PBaseMeasuregroupId != 0) {
				PBaseMeasuregroup measureGroup = measureUnitGroupDaoProxy
						.getMeasureGroupById(PBaseMeasuregroupId);
				m.setMeasureGroup(measureGroup);

				PBaseMeasureunit unit = new PBaseMeasureunit();
				unit.setMeasureGroupId(PBaseMeasuregroupId);
				PBaseMeasureunit measureUnit = new PBaseMeasureunit();
				if (measureUnitDaoProxy.getMeasureUnitByGroupId(unit).size() != 0) {
					measureUnit = measureUnitDaoProxy.getMeasureUnitByGroupId(
							unit).get(0);
				}
				m.setMeasureUnit(measureUnit);
			}
			if (PBaseAccountbookBySalesInputAccountBookId != 0) {
				PBaseAccountbook salesInputAccountBook = accountDaoProxy
						.getAccountbookById(PBaseAccountbookBySalesInputAccountBookId);
				m.setSalesInputAccountBook(salesInputAccountBook);
			}
			if (PBaseAccountbookByStorageAccountBookId != 0) {
				PBaseAccountbook storageAccountBook = accountDaoProxy
						.getAccountbookById(PBaseAccountbookByStorageAccountBookId);
				m.setStorageAccountBook(storageAccountBook);
			}
			if (PBaseVerifyprojectSupplierId != 0) {
				PBaseVerifyprojectSupplier supplier = supplierDaoProxy
						.getSupplierById(PBaseVerifyprojectSupplierId);
				m.setSupplier(supplier);
			}
			if (PBaseVerifyprojectStorageId != 0) {
				PBaseVerifyprojectStorage storage = storageDaoProxy
						.getStorageById(PBaseVerifyprojectStorageId);
				m.setStorage(storage);
			}
			if (PBaseVerifyprojectCandiateId != 0) {
				PBaseVerifyprojectCandiate candiate = candiateDaoProxy
						.getCandiateById(PBaseVerifyprojectCandiateId);
				m.setCandiate(candiate);
			}
			if (PBaseMaterialAssistantattributetypeId != 0) {
				/** 此处需要修改 */
				/** 王格滨2009-12-3修改添加* */
				// PBaseMaterialAssistantattributetype assistantattributetype =
				// new PBaseMaterialAssistantattributetype();
				PBaseMaterialAssistantattributetype assistantattributetype = assiatantAttributetypeDaoProxy
						.getAttrobuteTypeById(PBaseMaterialAssistantattributetypeId);
				// assistantattributetype.setAssistantAttributeTypeName("辅助资料");
				m.setAssistantattributetype(assistantattributetype);
			}
			if (PBaseAccountbookBySalesCostAccountBookId != 0) {
				PBaseAccountbook salesCostAccountBook = accountDaoProxy
						.getAccountbookById(PBaseAccountbookBySalesCostAccountBookId);
				m.setSalesCostAccountBook(salesCostAccountBook);
			}
			materials.add(m);
		}
		return materials;
	}

	@Override
	public void updateMaterial(PBaseVerifyprojectMaterial material)
			throws Throwable {
		// PBaseVerifyprojectMaterial pbvm =
		// materialDaoProxy.getMaterialById(material.getMaterialId());
		// if(material.getMaterialCode().equals(pbvm.getMaterialCode())){
		materialDaoProxy.updateMaterial(material);
		// }else{
		// if(candiateDaoProxy.isSame(material.getMaterialCode())){
		// throw new Throwable("代码【"+material.getMaterialCode()+"】已经存在");
		// }else{
		// materialDaoProxy.updateMaterial(material);
		// }
		// }
	}

	public List<MaterialDTO> getAllMaterial() throws Throwable {
		// TODO Auto-generated method stub
		return null;
	}

	public PBaseVerifyprojectMaterialDao getMaterialDaoProxy() {
		return materialDaoProxy;
	}

	public void setMaterialDaoProxy(
			PBaseVerifyprojectMaterialDao materialDaoProxy) {
		this.materialDaoProxy = materialDaoProxy;
	}

	public PBaseMeasureUnitGroupDAO getMeasureUnitGroupDaoProxy() {
		return measureUnitGroupDaoProxy;
	}

	public void setMeasureUnitGroupDaoProxy(
			PBaseMeasureUnitGroupDAO measureUnitGroupDaoProxy) {
		this.measureUnitGroupDaoProxy = measureUnitGroupDaoProxy;
	}

	public PBaseAccountbookDAO getAccountDaoProxy() {
		return accountDaoProxy;
	}

	public void setAccountDaoProxy(PBaseAccountbookDAO accountDaoProxy) {
		this.accountDaoProxy = accountDaoProxy;
	}

	public PBaseVerifyprojectCandiateDao getCandiateDaoProxy() {
		return candiateDaoProxy;
	}

	public void setCandiateDaoProxy(
			PBaseVerifyprojectCandiateDao candiateDaoProxy) {
		this.candiateDaoProxy = candiateDaoProxy;
	}

	public PBaseVerifyprojectStorageDao getStorageDaoProxy() {
		return storageDaoProxy;
	}

	public void setStorageDaoProxy(PBaseVerifyprojectStorageDao storageDaoProxy) {
		this.storageDaoProxy = storageDaoProxy;
	}

	public PBaseVerifyprogectSupplierDao getSupplierDaoProxy() {
		return supplierDaoProxy;
	}

	public void setSupplierDaoProxy(
			PBaseVerifyprogectSupplierDao supplierDaoProxy) {
		this.supplierDaoProxy = supplierDaoProxy;
	}

	/**
	 * @return the assiatantAttributetypeDaoProxy
	 */
	public AssistantAttributeTypeDao getAssiatantAttributetypeDaoProxy() {
		return assiatantAttributetypeDaoProxy;
	}

	/**
	 * @param assiatantAttributetypeDaoProxy
	 *            the assiatantAttributetypeDaoProxy to set
	 */
	public void setAssiatantAttributetypeDaoProxy(
			AssistantAttributeTypeDao assiatantAttributetypeDaoProxy) {
		this.assiatantAttributetypeDaoProxy = assiatantAttributetypeDaoProxy;
	}

}
